// pages/my/my.js
const app = getApp()
var request = require('../../utils/request.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    votes:"",
    avator:"",
    nickName:"xxxx"
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      avator: app.globalData.userInfo.avatarUrl,
      nickName: app.globalData.userInfo.nickName,
    })
    this.getVotes();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //获取所有的问卷
  getVotes: function () {
    let that = this;
    request.getData(
      "?type=vote",
      (data) => {
        that.setData({
          votes: data.data
        })
      },
      (error) => {

      }
    );
  },
  exportData: function () {
   
    let _that = this;
    wx.showLoading({
      title: '下载中',
    });
    wx.downloadFile({
      url: 'http://www.wx.com/?type=exportData',
      success: function (res) {
        var filePath = res.tempFilePath;
        console.log(res)
        wx.hideLoading();
        //页面显示加载动画
        wx.openDocument({
          filePath: filePath,
          success: function (res) {
            console.log('打开文档成功')
          }
        })
      }
    })
  },
  lookData:function(){
    //跳转到详情
    wx.navigateTo({
      url: '/pages/data/data'
    })
  },
  delVote(e){
    console.log(e);
    let id = e.currentTarget.dataset.id;
    let that = this;
    request.getData(
      "?type=del_vote&id="+id,
      (data) => {
       wx.showToast({
         title: '删除成功',
       })
        that.getVotes();
      },
      (error) => {

      }
    );
  },
  goCount(e){
    console.log( e.currentTarget.dataset.id)
    //跳转到详情
    wx.navigateTo({
      url: '/pages/count/count?id=' + e.currentTarget.dataset.id
    })
  }
})